package com.imufe.chemicalenterprisedangerwarning.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.imufe.chemicalenterprisedangerwarning.entity.query.CheckItemQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.query.CreateGroup;
import com.imufe.chemicalenterprisedangerwarning.entity.query.UpdateGroup;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.CheckItemVO;
import com.imufe.chemicalenterprisedangerwarning.exception.ValidationException;
import com.imufe.chemicalenterprisedangerwarning.service.CheckItemService;
import com.imufe.chemicalenterprisedangerwarning.common.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 检查条目表 前端控制器
 * </p>
 *
 * @author BaiBan
 * @since 2022-10-05
 */
@Api(tags = "检查条目")
@RestController
@RequestMapping("/check-item")
public class CheckItemController {

    @Autowired
    CheckItemService service;

    @ApiOperation(value = "检查条目列表", notes = "检查条目的列表(分页)")
    @GetMapping("/page")
    public ResponseEntity<IPage<CheckItemVO>> page(@RequestParam(value = "current", defaultValue = "1") Long current, String source) {
        IPage<CheckItemVO> page = service.page(current, source);
        return ResponseEntity.success(page);
    }

    @ApiOperation(value = "条目来源", notes = "条目源出处下拉选项列表")
    @GetMapping("/option/source/list")
    public ResponseEntity<List<String>> source() {
        return ResponseEntity.success(service.source());
    }

    @ApiOperation(value = "新增", notes = "新增一个检查条目，返回新增的检查条目信息")
    @PostMapping("/create")
    public ResponseEntity<CheckItemVO> create(@RequestBody @Validated(CreateGroup.class) CheckItemQuery query) throws ValidationException {
        CheckItemVO detailedVO = service.create(query);
        return ResponseEntity.success(detailedVO);
    }

    // 检查条目不允许删除

//    @ApiOperation(value = "删除", notes = "删除一个检查条目")
//    @DeleteMapping("/delete")
//    public ResponseEntity<?> delete(@RequestBody Long id) {
//        service.removeById(id);
//        return ResponseEntity.success();
//    }

    @ApiOperation(value = "修改", notes = "修改一个检查条目，返回修改后的检查条目信息")
    @PutMapping("/update")
    public ResponseEntity<CheckItemVO> update(@RequestBody @Validated(UpdateGroup.class) CheckItemQuery query) throws ValidationException {
        CheckItemVO detailedVO = service.update(query);
        return ResponseEntity.success(detailedVO);
    }
}